//Select 选择器
$(function() {
	$('.is-disabled').unbind('click');
})
$('.el-input').on('click', function(event) {
	$(this).toggleClass('is-focus');
	$('.el-input__icon').toggleClass('is-reverse');
	$('.el-select-dropdown').slideToggle(300);
	event.stopPropagation();
})
var sValue = $('.el-input__inner').val();
$('.el-select-dropdown__list .el-select-dropdown__item').on('click', function() {
	//	禁止点击事件
	$('.is-disabled').unbind('click');
	$('.el-input').removeClass('is-focus');
	$(this).addClass('selected').siblings().removeClass('selected');
	//	去空格
	let oText = $.trim($(this).text());
	$('.el-input__inner').val(oText);
	sValue = oText;
	$('.el-select-dropdown').slideUp(300);
	$('.el-input__icon').removeClass('is-reverse');
})

$(".el-input__suffix-inner i").on('mouseover', function() {
	if(sValue) {
		$(this).addClass('icon-chahao');
		$('.el-input__suffix-inner i').removeClass('icon-jiantou-copy-copy-copy');
		$('.icon-chahao').on('click', function(event) {
			$('.el-input__inner').val('');
			sValue = '';
			event.stopPropagation();
			$(".el-input__suffix-inner i").removeClass('icon-chahao');
			$('.el-input__suffix-inner i').addClass('icon-jiantou-copy-copy-copy');
			$('.el-select-dropdown__list .el-select-dropdown__item').removeClass('selected');
			$('.el-input__icon').removeClass('is-reverse');
			$('.el-select-dropdown').slideUp(300);
		})
	}
})
$(".el-input__suffix-inner i").on('mouseout', function() {
	if(sValue) {
		$(this).removeClass('icon-chahao');
		$('.el-input__suffix-inner i').addClass('icon-jiantou-copy-copy-copy');
	}
})

//顶部导航菜单
$('.sidebar-menu .tree-view').on('mouseenter', function() {
	if($(this).children('.treeview-menu').length != 0) {
		$(this).children('.treeview-menu').stop().slideDown('fast');
		//					$(this).find('i:first').stop().addClass('is-reverse');
	}
})
$('.sidebar-menu .tree-view').on('mouseleave', function() {
	var index = $(this).index();
	if($(this).children('.treeview-menu').length != 0) {
		$(this).children('.treeview-menu').stop().slideUp('fast');
		//					$(this).find('i').stop().removeClass('is-reverse');
	}
})
$('.sidebar-menu>.tree-view').on('click', function() {
	if($(this).children('.treeview-menu').length != 0) {
		return
	} else {
		$('.sidebar-menu .tree-view').removeClass('bomLine');
		$('.sidebar-menu .tree-view').children('a').removeClass('yellowCol');
		$(this).addClass('bomLine');
	}
})
$('.treeview-menu>.tree-view').on('click', function(event) {
	event.stopPropagation();
	if($(this).parents('.treeview-menu').length == 2) {
		$('.treeview-menu .tree-view>a').removeClass('yellowCol');
		$(this).parent().parent().removeClass('bomLine');
		$(this).children('a').addClass('yellowCol');
		$(this).parents('.one').addClass('bomLine');
	} else {
		$('.treeview-menu .tree-view>a').removeClass('yellowCol');
		$(this).children('a').addClass('yellowCol');
		$(this).parents('.tree-view').addClass('bomLine');
		$(this).parents('.tree-view').siblings().removeClass('bomLine');
	}

})

//上传图片
var arrImg = [];

function uploadImg(fileDom) {
	//				console.log('11',fileDom);
	//取到file文件的本地路径，就是你电脑上的存储路径
	//				console.log('22',fileDom.value);
	// 获取图片数据对象
	var file = fileDom.files[0];
	// 获取图片名字
	//				console.log('333',file.name);
	// 获取图片文件大小,单位为byte
	//				console.log('44',file.size);
	var reader = new FileReader();
	reader.readAsDataURL(file);
	//确保文件成功获取，base64数据量比较大
	reader.onload = function(event) {
		var e = event || window.event
		//					var img = document.getElementById("viewImg");
		var oSrc = e.target.result; //存地址
		arrImg.push(oSrc);
		console.log('aa1', arrImg);
		//或者 img.src = this.result; 因为e.target == this
		var omainImg = document.getElementById("mainImg");
		var img = document.createElement('img');
		img.style.width = 200 + 'px';
		img.style.height = 100 + 'px';
		for(var i = 0; i < arrImg.length; i++) {
			img.src = arrImg[i];
			omainImg.appendChild(img);
		}
	}
}
//本地储存
// cookie    存放数据 4k左右   跟 服务器端通信 ,设置有效时间, 过了有效时间  数据 消失
// localStorage 存放数据 5M 至 更大 不跟 服务器端通信 除非数据被清除,否则一直存在
// sessionStorage 存放数据 5M 至 更大 不跟 服务器端通信 关闭页面 数据 消失
// 三者都保存在浏览器端,跟服务器端的session机制不同
// function saveStorage(id){
// 	var target = document.getElementById(id);
// 	var str = target.value;
// 	sessionStorage.setItem('key1',str);
// }
// function loadStorage(id){
// 	var target = document.getElementById(id);
// 	var str = sessionStorage.getItem('key1');
// 	target.innerHTML = str ;
// }

function saveStorage(id) {
	var target = document.getElementById(id);
	var str = target.value;
	localStorage.setItem('key1', str);
}

function loadStorage(id) {
	var target = document.getElementById(id);
	var str = localStorage.getItem('key1');
	target.innerHTML = str;
}
//表单提交错误提示
function stipsTr(msg) {
	$('.el-strong').text(msg);
	$('#alert').fadeIn('fast');
	setTimeout(() => {
		$('#alert').fadeOut('fast');
	}, 2000)
}

//错误提示动画
function etips(msg) {
	$('.etipsmsg').text(msg)
	$('.errortips').fadeIn('fast');
	setTimeout(function() {
		$('.errortips').fadeOut('fast')
	}, 2000)
}
//正确提示动画
function stips(msg) {
	$('.stipsmsg').text(msg)
	$('.successtips').fadeIn('fast');
	setTimeout(function() {
		$('.successtips').fadeOut('fast')
	}, 2000)
}
//下拉菜单过滤
/* 点击按钮可以显示或隐藏下拉菜单 */
function myFunction() {
	document.getElementById("myDropdown").classList.toggle("show");
}

function filterFunction() {
	var input, filter, ul, li, a, i;
	input = document.getElementById("myInput");
	filter = input.value.toUpperCase();
	div = document.getElementById("myDropdown");
	a = div.getElementsByTagName("a");
	for(i = 0; i < a.length; i++) {
		if(a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
			a[i].style.display = "";
		} else {
			a[i].style.display = "none";
		}
	}
}

function autocomplete(inp, arr) {
	/*函数主要有两个参数：文本框元素和自动补齐的完整数据*/
	var currentFocus;
	/* 监听 - 在写入时触发 */
	inp.addEventListener("input", function(e) {
		var a, b, i, val = this.value;
		/*关闭已经打开的自动完成值列表*/
		closeAllLists();
		if(!val) {
			return false;
		}
		currentFocus = -1;
		/*创建列表*/
		a = document.createElement("DIV");
		a.setAttribute("id", this.id + "autocomplete-list");
		a.setAttribute("class", "autocomplete-items");
		/*添加 DIV 元素*/
		this.parentNode.appendChild(a);
		/*循环数组...*/
		for(i = 0; i < arr.length; i++) {
			/*检查选项是否以与文本字段值相同的字母开头*/
			if(arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
				/*为匹配元素创建 DIV*/
				b = document.createElement("DIV");
				/*使匹配字母变粗体*/
				b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
				b.innerHTML += arr[i].substr(val.length);
				/*insert a input field that will hold the current array item's value:*/
				b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
				/*execute a function when someone clicks on the item value (DIV element):*/
				b.addEventListener("click", function(e) {
					/*insert the value for the autocomplete text field:*/
					inp.value = this.getElementsByTagName("input")[0].value;
					/*close the list of autocompleted values,
					(or any other open lists of autocompleted values:*/
					closeAllLists();
				});
				a.appendChild(b);
			}
		}
	});
	/*execute a function presses a key on the keyboard:*/
	inp.addEventListener("keydown", function(e) {
		var x = document.getElementById(this.id + "autocomplete-list");
		if(x) x = x.getElementsByTagName("div");
		if(e.keyCode == 40) {
			/*If the arrow DOWN key is pressed,
			increase the currentFocus variable:*/
			currentFocus++;
			/*and and make the current item more visible:*/
			addActive(x);
		} else if(e.keyCode == 38) { //up
			/*If the arrow UP key is pressed,
			decrease the currentFocus variable:*/
			currentFocus--;
			/*and and make the current item more visible:*/
			addActive(x);
		} else if(e.keyCode == 13) {
			/*If the ENTER key is pressed, prevent the form from being submitted,*/
			e.preventDefault();
			if(currentFocus > -1) {
				/*and simulate a click on the "active" item:*/
				if(x) x[currentFocus].click();
			}
		}
	});

	function addActive(x) {
		/*a function to classify an item as "active":*/
		if(!x) return false;
		/*start by removing the "active" class on all items:*/
		removeActive(x);
		if(currentFocus >= x.length) currentFocus = 0;
		if(currentFocus < 0) currentFocus = (x.length - 1);
		/*add class "autocomplete-active":*/
		x[currentFocus].classList.add("autocomplete-active");
	}

	function removeActive(x) {
		/*a function to remove the "active" class from all autocomplete items:*/
		for(var i = 0; i < x.length; i++) {
			x[i].classList.remove("autocomplete-active");
		}
	}

	function closeAllLists(elmnt) {
		/*close all autocomplete lists in the document,
		except the one passed as an argument:*/
		var x = document.getElementsByClassName("autocomplete-items");
		for(var i = 0; i < x.length; i++) {
			if(elmnt != x[i] && elmnt != inp) {
				x[i].parentNode.removeChild(x[i]);
			}
		}
	}
	/*execute a function when someone clicks in the document:*/
	document.addEventListener("click", function(e) {
		closeAllLists(e.target);
	});
}

/*数组 - 包含所有国家或地区名*/
var countries = ["Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Anguilla", "Antigua & Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia & Herzegovina", "Botswana", "Brazil", "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central Arfrican Republic", "Chad", "Chile", "China", "Colombia", "Congo", "Cook Islands", "Costa Rica", "Cote D Ivoire", "Croatia", "Cuba", "Curacao", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands", "Faroe Islands", "Fiji", "Finland", "France", "French Polynesia", "French West Indies", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guam", "Guatemala", "Guernsey", "Guinea", "Guinea Bissau", "Guyana", "Haiti", "Honduras", "Hong Kong China", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Isle of Man", "Israel", "Italy", "Jamaica", "Japan", "Jersey", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kosovo", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau China", "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauro", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "North Korea", "Norway", "Oman", "Pakistan", "Palau", "Palestine", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russia", "Rwanda", "Saint Pierre & Miquelon", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Korea", "South Sudan", "Spain", "Sri Lanka", "St Kitts & Nevis", "St Lucia", "St Vincent", "Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan China", "Tajikistan", "Tanzania", "Thailand", "Timor L'Este", "Togo", "Tonga", "Trinidad & Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks & Caicos", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States of America", "Uruguay", "Uzbekistan", "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Virgin Islands (US)", "Yemen", "Zambia", "Zimbabwe"];

/*传递参数*/
autocomplete(document.getElementById("myInput"), countries);